import urllib2
import os   
import datetime   
import string   
import re   
import unittest

VOA_URL=r'    http://www.unsv.com/'  

def getFileNameFromLink(strLink):   
    filename_re=r'[^/:]+.mp3'  
    matched=re.search(filename_re, strLink)   
    if matched:   
        return matched.group(0)   
    else:   
        return r''  

def genMp3DownLinkRe(date):   
    return r'http[^"]*'+date+r'[^"]*\.mp3'  

def genDateStr(rawStr):   
    return string.replace(rawStr, r'-', r'')   

def getMp3Links(aRe):   
print r'    Reading VOA page...',   
    response=urllib2.urlopen(VOA_URL)   
    html=response.read()   
    print r'    Done.'  
    print r'    Trying to parse the page and get the right mp3 links...',   
    matchedLinks=re.findall(aRe, html)   
    print r'    Done.'  
    return matchedLinks   

def downloadVOA(strDay):
    if not os.path.isdir(strDay):   
        print r"    Can't find the date directory, create one...",   
        os.mkdir(strDay)   
        print r'    Done'  
    
    link_re = genMp3DownLinkRe(strDay)   
    mp3Links = getMp3Links(link_re)
    
    print r'    link_re = %s'%link_re
    
    mp3LinksSet = set()   
    for link in mp3Links:   
        mp3LinksSet.add(link)   

    print r'    Found %d link, begin to download them...'%len(mp3LinksSet)   
    
    for link in mp3LinksSet:   
        print r'    Handling link %s...'%link   
        filename=getFileNameFromLink(link)   
        if not filename==r'':               
            wholename=os.path.join(strDay, filename)   
            
            if os.path.isfile(wholename):   
                print '    \tThe file has been downloaded. Skip this file.'  
                continue  
            print '    \tDownloading the mp3 file...',   
            data=urllib2.urlopen(link).read()   
            print '    Done'  
            print '    \tWriting data info file...',   
            f=file(wholename, 'wb')   
            f.write(data)   
            print 'Done'  
            f.close()   
            
if __name__ == '__main__':

    #Get today's data string   
    print r'[XXX]Starting excuting downloadVOA...%s'%str(datetime.datetime.now())
    
    for datnum in range(0, 5):
        str_today = genDateStr(str(datetime.date.today() - datetime.timedelta(days = datnum)))
        print r'    Today = %s'%str_today
        downloadVOA(str_today)
    print '[XXX]All Finished. ^_^...%s'%str(datetime.datetime.now())
